package cn.stylefeng.guns.modular.cb.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.entity.CbIndustry;
import cn.stylefeng.guns.modular.cb.mapper.CbIndustryMapper;
import cn.stylefeng.guns.modular.cb.model.params.CbIndustryParam;
import cn.stylefeng.guns.modular.cb.model.result.CbIndustryResult;
import  cn.stylefeng.guns.modular.cb.service.CbIndustryService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wj
 * @since 2021-01-15
 */
@Service
public class CbIndustryServiceImpl extends ServiceImpl<CbIndustryMapper, CbIndustry> implements CbIndustryService {

    @Override
    public void add(CbIndustryParam param){
        CbIndustry entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CbIndustryParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CbIndustryParam param){
        CbIndustry oldEntity = getOldEntity(param);
        CbIndustry newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CbIndustryResult findBySpec(CbIndustryParam param){
        return null;
    }

    @Override
    public List<CbIndustryResult> findListBySpec(CbIndustryParam param){

        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(CbIndustryParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CbIndustryParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CbIndustry getOldEntity(CbIndustryParam param) {
        return this.getById(getKey(param));
    }

    private CbIndustry getEntity(CbIndustryParam param) {
        CbIndustry entity = new CbIndustry();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
